package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RequestMapping("/admin/dish")
@RestController
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PutMapping("")
    @ApiOperation("修改菜品")
    public Result modifydish( @RequestBody DishDTO dishDTO) {
        log.info("修改菜品{}:", dishDTO);
        dishService.modifydish(dishDTO);
        //修改菜品清除缓存
        cleanRedis("dish_*");
        return Result.success();
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping("")
    @ApiOperation("删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids) {
        log.info("删除菜品{}:",ids);
      dishService.deleteByid(ids);
        //修改菜品清除缓存
        cleanRedis("dish_*");
        return Result.success();
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
      @PostMapping("")
      @ApiOperation("新增菜品")
    public Result sava(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品{}:", dishDTO);
        dishService.sava(dishDTO);
        //清除当前分类
          cleanRedis("dish_"+dishDTO.getCategoryId());
        return Result.success();
      }

        @GetMapping("/{id}")
        @ApiOperation("根据id来查询菜品")
      public Result<DishVO> selectDishById(@PathVariable Long id) {
          log.info("根据id来查询菜品{}:",id);
           DishVO dishVO = dishService.selectDishById(id);
           return Result.success(dishVO);

      }

    /**
     * 根据分类ID 来查询
     * @return
     */
    @ApiOperation("根据分类id来查询")
    @GetMapping("/list")
      public Result<List<Dish>> listDish(Integer categoryId) {
        log.info("根据分类id来查询{}:",categoryId);
     List<Dish> listDish = dishService.listDish(categoryId);
     return Result.success(listDish);
      }


    /**
     * 分页查询
     * @param queryDTO
     * @return
     */
      @GetMapping("/page")
      @ApiOperation("分页查询")
      public Result<PageResult> pagelist(DishPageQueryDTO queryDTO) {
        log.info("分页查询{}:",queryDTO);
        PageResult result =dishService.pagelist(queryDTO);
        return Result.success(result);
      }


      @ApiOperation("修改状态")
      @PostMapping("/status/{status}")
      public Result modifyStatus(@PathVariable Integer status,Long id) {
          log.info("修改状态{}:",id);
          dishService.modifyStatus(status,id);
          //修改菜品清除缓存
          cleanRedis("dish_*");
          return Result.success();
      }


      public void cleanRedis(String pattern){
          Set keys = redisTemplate.keys(pattern);
          redisTemplate.delete(keys);

      }
}
